/*!
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */

$(document).ready(function() {
	alert('1');
    if($("#link").val()){
        $('#linkBody').show();
        $('#url').attr("checked", true);
    }
	$("#title").focus();
	$("#inputForm").validate({
		submitHandler: function(form){
            if ($("#categoryId").val()==""){
                $("#categoryName").focus();
                top.$.jBox.tip('请选择归属栏目','warning');
            }else if (CKEDITOR.instances.content.getData()=="" && $("#link").val().trim()==""){
                top.$.jBox.tip('请填写正文','warning');
            }else{
                loading('正在提交，请稍等...');
                form.submit();
            }
		},
		errorContainer: "#messageBox",
		errorPlacement: function(error, element) {
			$("#messageBox").text("输入有误，请先更正。");
			if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
				error.appendTo(element.parent().parent());
			} else {
				error.insertAfter(element);
			}
		}
	});
})

/*---------------------------------------------*/

var articleSelect = [];
function articleSelectAddOrDel(id,title){
	alert('2');
	var isExtents = false, index = 0;
	for (var i=0; i<articleSelect.length; i++){
		if (articleSelect[i][0]==id){
			isExtents = true;
			index = i;
		}
	}
	if(isExtents){
		articleSelect.splice(index,1);
	}else{
		articleSelect.push([id,title]);
	}
	articleSelectRefresh();
}
function articleSelectRefresh(){
	alert('3');
	$("#articleDataRelation").val("");
	$("#articleSelectList").children().remove();
	for (var i=0; i<articleSelect.length; i++){
		$("#articleSelectList").append("<li>"+articleSelect[i][1]+
				"&nbsp;&nbsp;<a href=\"javascript:\" " +
				"onclick=\"articleSelectAddOrDel('"+articleSelect[i][0]+"','"+articleSelect[i][1]+"');\">×</a></li>");
		$("#articleDataRelation").val($("#articleDataRelation").val()+articleSelect[i][0]+",");
	}
}

function articleSelectListLoad(){
	articleSelectRefresh();
	alert('4');
	$.getJSON("${ctx}/cms/article/findByIds",{ids:$("#articleDataRelation").val()},function(data){
		for (var i=0; i<data.length; i++){
			articleSelect.push([data[i][1],data[i][2]]);
		}
		articleSelectRefresh();
	});
	alert('5');
	$("#relationButton").click(function(){
		top.$.jBox.open("iframe:${ctx}/cms/article/selectList?pageSize=8", "添加相关",$(top.document).width()-220,$(top.document).height()-180,{
			buttons:{"确定":true}, loaded:function(h){
				$(".jbox-content", top.document).css("overflow-y","hidden");
			}
		});
	});
}

/*---------------------------------------------*/

function viewComment(href){
	top.$.jBox.open('iframe:'+href,'查看评论',$(top.document).width()-220,$(top.document).height()-180,{
		buttons:{"关闭":true},
		loaded:function(h){
			$(".jbox-content", top.document).css("overflow-y","hidden");
			$(".nav,.form-actions,[class=btn]", h.find("iframe").contents()).hide();
			$("body", h.find("iframe").contents()).css("margin","10px");
		}
	});
	return false;
}
